import { ref, reactive, readonly, onUnmounted } from 'vue'
/**
 * 拍照控制器 - Vue 3 组合式 API 版本
 * @param {Function} takePhotoFn - 实际拍照的异步函数
 * @param {Object} [options] - 配置选项
 * @param {number} [options.defaultCount=4] - 默认连续拍照次数
 * @param {number} [options.defaultInterval=3000] - 默认拍照间隔(ms)
 * @returns {Object} 拍照控制器对象
 */
export function usePhotoCapture(takePhotoFn, options = {}) {
  // 响应式配置
  const config = reactive({
    defaultCount: 4,
    defaultInterval: 3000,
    countdownSeconds: 3, // 新增：倒计时秒数
    ...options
  })

  // 响应式状态
  const isContinuousMode = ref(false)
  const isCapturing = ref(false)
  const captureCount = ref(0)
  const stopFlag = ref(false)
  const countdown = ref(0) // 新增：当前倒计时秒数
  const countdownTimer = ref(null) // 新增：倒计时定时器
  const captureTimer = ref(null)

  // 清理所有定时器
  const clearAllTimers = () => {
    clearInterval(captureTimer.value)
    clearTimeout(countdownTimer.value)
    captureTimer.value = null
    countdownTimer.value = null
  }

  // 设置倒计时
  const setSeconds = (seconds) => {
    config.countdownSeconds = seconds
  } 

  // 倒计时函数
  const startCountdown = (seconds = config.countdownSeconds) => {
    return new Promise(resolve => {
      countdown.value = seconds
      
      const count = () => {
        if (stopFlag.value) {
          countdown.value = 0
          resolve(false)
          return
        }
        
        countdown.value--
        
        if (countdown.value > 0) {
          countdownTimer.value = setTimeout(count, 1000)
        } else {
          resolve(true)
        }
      }
      
      countdownTimer.value = setTimeout(count, 1000)
    })
  }

  // 单次拍照
  const captureOnce = async () => {
    if (isCapturing.value) return
    
    try {
      isCapturing.value = true
      await takePhotoFn()
    } catch (error) {
      console.error('拍照失败:', error)
      throw error
    } finally {
      isCapturing.value = false
    }
  }

  // 连续拍照
  const captureContinuous = async (count = config.defaultCount, interval = config.defaultInterval) => {
    if (isCapturing.value) return
    
    isContinuousMode.value = true
    isCapturing.value = true
    captureCount.value = 0
    stopFlag.value = false
    
    try {
      while (captureCount.value < count && !stopFlag.value) {
        // 执行倒计时
        const shouldCapture = await startCountdown()
        if (!shouldCapture) break
        
        // 执行拍照
        await takePhotoFn()
        captureCount.value++
        
        // 最后一次拍照后不等待
        if (captureCount.value >= count) break
        
        // 等待间隔时间
        await new Promise(resolve => {
          captureTimer.value = setTimeout(resolve, interval)
        })
      }
    } catch (error) {
      console.error('连续拍照失败:', error)
      throw error
    } finally {
      clearAllTimers()
      isCapturing.value = false
      isContinuousMode.value = false
      countdown.value = 0
    }
  }

  // 自定义拍照
  const captureCustom = async (params) => {
    if (isCapturing.value) return
    
    try {
      isCapturing.value = true
      await takePhotoFn(params)
    } catch (error) {
      console.error('自定义拍照失败:', error)
      throw error
    } finally {
      isCapturing.value = false
    }
  }

  // 停止拍照
  const stopCapture = () => {
    stopFlag.value = true
    clearAllTimers()
    isCapturing.value = false
    isContinuousMode.value = false
    countdown.value = 0
  }

  // 组件卸载时清理
  onUnmounted(() => {
    clearAllTimers()
  })

  // 暴露只读状态和方法
  return {
    // 状态
    isCapturing: readonly(isCapturing),
    isContinuousMode: readonly(isContinuousMode),
    captureCount: readonly(captureCount),
    countdown: readonly(countdown), // 新增：倒计时状态
    
    // 方法
    captureOnce,
    captureContinuous,
    captureCustom,
    stopCapture,
    setSeconds
  }
}
